package com.ruoyi.program.service.impl;

import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.program.mapper.WxLearningMaterialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.program.mapper.WxChapterMapper;
import com.ruoyi.program.domain.WxChapter;
import com.ruoyi.program.domain.WxLearningMaterial;
import com.ruoyi.program.service.IWxChapterService;

/**
 * 章节管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-18
 */
@Service
public class WxChapterServiceImpl implements IWxChapterService
{
    @Autowired
    private WxChapterMapper wxChapterMapper;

    @Autowired
    private WxLearningMaterialMapper wxLearningMaterialMapper;

    /**
     * 查询章节管理
     *
     * @param chapterId 章节管理主键
     * @return 章节管理
     */
    @Override
    public WxChapter selectWxChapterByChapterId(Long chapterId)
    {
        return wxChapterMapper.selectWxChapterByChapterId(chapterId);
    }

    /**
     * 查询章节管理列表
     *
     * @param wxChapter 章节管理
     * @return 章节管理
     */
    @Override
    public List<WxChapter> selectWxChapterList(WxChapter wxChapter)
    {
        List<WxChapter> all = wxChapterMapper.selectWxChapterList(wxChapter);
        if (CollUtil.isEmpty(all)) {
            return Collections.emptyList();
        }

        /* 2. 遍历 → 每条章节单独查资料并回填 */
        for (WxChapter ch : all) {
            List<WxLearningMaterial> mats = wxLearningMaterialMapper.selectByChapterId(ch.getChapterId());
            ch.setMaterials(mats);
        }

        /* 3. 建树（原逻辑不动） */
        Map<Long, WxChapter> map = new HashMap<>(all.size());
        all.forEach(c -> map.put(c.getChapterId(), c));

        List<WxChapter> root = new ArrayList<>();
        for (WxChapter node : all) {
            if (node.getParentId() == null || node.getParentId() == 0L) {
                root.add(node);
            } else {
                WxChapter parent = map.get(node.getParentId());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        /* 4. 排序 */
        Comparator<WxChapter> c = Comparator.comparing(WxChapter::getSort);
        root.sort(c);
        root.forEach(this::sortRecursive);

        return root;
    }


    /* 递归排序子节点 */
    private void sortRecursive(WxChapter chapter) {
        if (CollectionUtil.isNotEmpty(chapter.getChildren())) {
            chapter.getChildren().sort(Comparator.comparing(WxChapter::getSort));
            chapter.getChildren().forEach(this::sortRecursive);
        }
    }

    /**
     * 新增章节管理
     *
     * @param wxChapter 章节管理
     * @return 结果
     */
    @Override
    public int insertWxChapter(WxChapter wxChapter)
    {
        wxChapter.setCreateTime(DateUtils.getNowDate());
        Long loginUserId = SecurityUtils.getLoginUser().getUserId();
        wxChapter.setUserId(loginUserId);
        return wxChapterMapper.insertWxChapter(wxChapter);
    }

    /**
     * 修改章节管理
     *
     * @param wxChapter 章节管理
     * @return 结果
     */
    @Override
    public int updateWxChapter(WxChapter wxChapter)
    {
        wxChapter.setUpdateTime(DateUtils.getNowDate());
        return wxChapterMapper.updateWxChapter(wxChapter);
    }

    /**
     * 批量删除章节管理
     *
     * @param chapterIds 需要删除的章节管理主键
     * @return 结果
     */
    @Override
    public int deleteWxChapterByChapterIds(Long[] chapterIds)
    {
        return wxChapterMapper.deleteWxChapterByChapterIds(chapterIds);
    }

    /**
     * 删除章节管理信息
     *
     * @param chapterId 章节管理主键
     * @return 结果
     */
    @Override
    public int deleteWxChapterByChapterId(Long chapterId)
    {
        return wxChapterMapper.deleteWxChapterByChapterId(chapterId);
    }
}
